package m2;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @author Chenwx
 * @date 2021/5/13 17:22
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //  创建消费者
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("consumer-group-2");
        //设置name server
        c.setNamesrvAddr("192.168.64.141:9876");
        //从 Topic2 订阅所有消息
        c.subscribe("Topic2","*");
        //消息监听器
        //Orderly 监听器，单线程接收处理消息
        c.setMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
                for (MessageExt s : msgs) {
                    String msg = new String(s.getBody());
                    System.out.println("收到： "+msg);
                }

                return ConsumeOrderlyStatus.SUCCESS;
                //return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
            }
        });

        //启动
        c.start();
    }

}
